package com.mall.server.controller.pms;


import cn.hutool.core.convert.Convert;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.model.vo.ProductCategoryWithChildrenVo;
import com.mall.server.service.pms.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService categoryService;

    @PostMapping("/create")
    public CommonResult addProductCategory(@RequestBody PmsProductCategory productCategory) {

        categoryService.addProductCategory(productCategory);
        return CommonResult.success(null);
    }


    @PostMapping("/delete/{id}")
    public CommonResult delProductCategory(@PathVariable("id") Integer id) {

        categoryService.delProductCategory(Convert.toLong(id));
        return CommonResult.success(null);
    }


    @PostMapping("/update/navStatus")
    public CommonResult updateNavStatus(Long[] ids, Integer navStatus) {

        categoryService.updateNavStatus(ids, navStatus);
        return CommonResult.success(null);
    }

    @PostMapping("/update/showStatus")
    public CommonResult updateShowStatus(Long[] ids, Integer showStatus) {

        categoryService.updateShowStatus(ids, showStatus);
        return CommonResult.success(null);
    }

    @PostMapping("/update/{id}")
    public CommonResult updateById(@PathVariable("id") Integer id, @RequestBody PmsProductCategory productCategory) {

        categoryService.updateById(id, productCategory);
        return CommonResult.success(null);
    }

    @GetMapping("/{id}")
    public CommonResult selectById(@PathVariable("id") Integer id) {

        PmsProductCategory category = categoryService.selectById(id);
        return CommonResult.success(category);
    }

    @GetMapping("/list/{parentId}")
    public CommonResult findParentId(@PathVariable("parentId") Integer parentId,
                                     @RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage<PmsProductCategory> pageInfo = categoryService.findParentId(Convert.toLong(parentId), pageNum, pageSize);
        return CommonResult.success(pageInfo);
    }

    @GetMapping("/list/withChildren")
    public CommonResult selectListWithChildren() {

        List<ProductCategoryWithChildrenVo> list = categoryService.selectListWithChildren();
        return CommonResult.success(list);
    }

}
